package com.seavenois.alife2;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.DisplayMetrics;
import android.view.SurfaceHolder;
import android.view.View;

/*************************************************/
/* The Game view *********************************/
/*************************************************/
/* Controls all the graphics. ********************/
/*************************************************/
@SuppressLint("ViewConstructor") //I rather use my own constructor
class GameView extends View implements SurfaceHolder.Callback {
	
	//Global values
	private byte scenario;
	private Bitmap bgImage;						//The background image
	private int bgWidth, bgHeight;				//Background (and board) dimensions
	private int screenWidth, screenHeight;		//Screen dimensions
	private int scrollX;						//Current horizontal scroll
	private int scrollY;						//Current vertical scroll
	//Basic data text (although they are text, they will be drawn)
	private Paint textYear = new Paint();
	private Paint textMoney = new Paint();
	private Paint textSouls = new Paint();
	private String msgYear, msgMoney, msgSouls;
	//The activity context
	private Context context;
	//Each one of the boxes
	public Box[] box = new Box[144];
	//Drawables for each box
	public Bitmap[] draw = new Bitmap[144];

	//Initializes each one of the boxes, setting its coordinates and if they are on a river
	private void initializeBoxes(){
		//Variables for loops
		int initx, inity, w, h;
		//Variables for the coordinates
		int tx, ty, lx, ly, rx, ry, bx, by;
		//Those values are measured from the background image
		//Don't change unless the background is changed
		//TODO: Set those values from Values.java
		initx = (int) (0.5 * bgWidth);
		inity = (int) (0.1954 * bgHeight);
		w = (int) (0.0760 * bgWidth);
		h = (int) (0.0533 * bgHeight);
		//Actually initialize them
		int idx = 0, ix, iy;
		for (iy = 0; iy < 12; iy ++){
			for (ix = 0; ix < 12; ix ++){
				tx = (int) (initx - iy * 0.5 * w + ix * 0.5 * w);
				ty = (int) (inity + iy * 0.5 * h + ix * 0.5 * h);
				lx = tx - w / 2;
				ly = ty + h / 2;
				rx = lx + w;
				ry = ly;
				bx = tx;
				by = ty + h;
				box[idx] = new Box(tx, ty, lx, ly, rx, ry, bx, by);
				idx ++;
			}
		}
		//Set river boxes
		switch (scenario){
			case Values.SCENARIO_HELL:
				box[7].setRiver(true);
				box[8].setRiver(true);
				box[9].setRiver(true);
				box[19].setRiver(true);
				box[20].setRiver(true);
				box[21].setRiver(true);
				box[31].setRiver(true);
				box[32].setRiver(true);
				box[33].setRiver(true);
				box[38].setRiver(true);
				box[39].setRiver(true);
				box[40].setRiver(true);
				box[41].setRiver(true);
				box[42].setRiver(true);
				box[43].setRiver(true);
				box[44].setRiver(true);
				box[45].setRiver(true);
				box[48].setRiver(true);
				box[49].setRiver(true);
				box[50].setRiver(true);
				box[51].setRiver(true);
				box[52].setRiver(true);
				box[53].setRiver(true);
				box[54].setRiver(true);
				box[55].setRiver(true);
				box[56].setRiver(true);
				box[57].setRiver(true);
				box[60].setRiver(true);
				box[61].setRiver(true);
				box[62].setRiver(true);
				box[63].setRiver(true);
				box[67].setRiver(true);
				box[68].setRiver(true);
				box[69].setRiver(true);
				box[72].setRiver(true);
				box[73].setRiver(true);
				box[79].setRiver(true);
				box[80].setRiver(true);
				box[81].setRiver(true);
				box[91].setRiver(true);
				box[91].setRiver(true);
				box[92].setRiver(true);
				box[93].setRiver(true);
				box[101].setRiver(true);
				box[102].setRiver(true);
				box[103].setRiver(true);
				box[104].setRiver(true);
				box[105].setRiver(true);
				box[112].setRiver(true);
				box[113].setRiver(true);
				box[114].setRiver(true);
				box[115].setRiver(true);
				box[116].setRiver(true);
				box[124].setRiver(true);
				box[125].setRiver(true);
				box[126].setRiver(true);
				box[136].setRiver(true);
				box[137].setRiver(true);
				box[138].setRiver(true);
				break;
			case Values.SCENARIO_HEAVEN:
				//TODO: Redo
				box[4].setRiver(true);
				box[5].setRiver(true);
				box[6].setRiver(true);
				box[16].setRiver(true);
				box[17].setRiver(true);
				box[18].setRiver(true);
				
				box[27].setRiver(true);
				box[28].setRiver(true);
				box[29].setRiver(true);
				
				box[39].setRiver(true);
				box[40].setRiver(true);
				box[41].setRiver(true);
				
				box[51].setRiver(true);
				box[52].setRiver(true);
				
				box[63].setRiver(true);
				box[64].setRiver(true);
				box[75].setRiver(true);
				box[76].setRiver(true);
				
				box[84].setRiver(true);
				box[85].setRiver(true);
				box[86].setRiver(true);
				box[87].setRiver(true);
				box[88].setRiver(true);
				box[89].setRiver(true);
				box[90].setRiver(true);
				box[91].setRiver(true);
				box[92].setRiver(true);
				box[93].setRiver(true);
				box[94].setRiver(true);
				box[96].setRiver(true);
				box[97].setRiver(true);
				box[98].setRiver(true);
				box[99].setRiver(true);
				box[100].setRiver(true);
				box[101].setRiver(true);
				box[102].setRiver(true);
				box[103].setRiver(true);
				box[104].setRiver(true);
				box[105].setRiver(true);
				box[106].setRiver(true);
				box[108].setRiver(true);
				box[109].setRiver(true);
				box[110].setRiver(true);
				box[111].setRiver(true);
				box[112].setRiver(true);
				box[113].setRiver(true);
				box[114].setRiver(true);
				box[115].setRiver(true);
				box[116].setRiver(true);
				box[117].setRiver(true);
				
				box[114].setRiver(true);
				box[115].setRiver(true);
				box[116].setRiver(true);
				box[124].setRiver(true);
				box[125].setRiver(true);
				box[126].setRiver(true);
				box[136].setRiver(true);
				box[137].setRiver(true);
				box[138].setRiver(true);
				break;
			
		}
	}
	
	//Constructor
	public GameView(Context c, byte s)
	{
		super(c);
		//Set the context
		context = c;
		//Set the scenario
		scenario = s;
		//Set and measure the background
		switch (scenario){
			case Values.SCENARIO_HEAVEN:
				bgImage = BitmapFactory.decodeResource(getResources(), R.drawable.boardheaven);
				break;
			case Values.SCENARIO_HELL:
				bgImage = BitmapFactory.decodeResource(getResources(), R.drawable.boardhell);
				break;
		}
		bgWidth = bgImage.getWidth();
		bgHeight = bgImage.getHeight();
		//Measure screen
		DisplayMetrics metrics = context.getResources().getDisplayMetrics();
		screenWidth = metrics.widthPixels;
		screenHeight = metrics.heightPixels;
		//Set initial scroll to 50%
		scrollX = (bgWidth - screenWidth) / 2;
		scrollY = (bgHeight - screenHeight) / 2;
		//Configure texts for year, souls, and money
		textYear.setStyle(Paint.Style.FILL);
        textYear.setColor(Color.WHITE);
        textYear.setTextSize(25);
        textYear.setAntiAlias(true);
        textYear.setTypeface(Typeface.MONOSPACE);
        textMoney.setStyle(Paint.Style.FILL);
        textMoney.setColor(Color.WHITE);
        textMoney.setTextSize(25);
        textMoney.setAntiAlias(true);
        textMoney.setTypeface(Typeface.MONOSPACE);
        textSouls.setStyle(Paint.Style.FILL);
        textSouls.setColor(Color.WHITE);
        textSouls.setTextSize(25);
        textSouls.setAntiAlias(true);
        textSouls.setTypeface(Typeface.MONOSPACE);
        //Update values for money, souls and year text
        updateMsg(0, 0, 0);
        //Initialize board boxes
        initializeBoxes();
	}
	
	//Update values for money, souls and year
	public void updateMsg(int y, int m, int s){
		msgMoney = context.getString(R.string.money) + ": " + Integer.toString(m)  + "ł";
		msgYear = context.getString(R.string.year) + ": " + Integer.toString(y);
		msgSouls = context.getString(R.string.souls) + ": " + Integer.toString(s);
	}
	
	//Called from activity when something is built, upgraded or destroyed.
	//Sets the drawable, and if its a new building (not and upgrade) sets it's age to 0
	public void build(int _box, int _building, boolean upgrade){
		int didx = R.drawable.building_unknown;
		if (upgrade == false)
			box[_box].resetAge();
		box[_box].setBuilding(_building);
		//Switch the building to be drawn
		switch (_building){
			//Level 1 areas. Same drawable for Hell and Heaven
			//Must switch box age
			case Values.BUILDING_MAROON_1:
				if (box[_box].getAge() < Values.AGE_1)
					didx = R.drawable.building_maroon_0;
				else{
					switch (scenario){
						case Values.SCENARIO_HELL:
							didx = R.drawable.building_maroon_1_hell;
							break;
						case Values.SCENARIO_HEAVEN:
							didx = R.drawable.building_maroon_1_heaven;
							break;
					}
				}
				break;
			case Values.BUILDING_PINK_1:
				if (box[_box].getAge() < Values.AGE_1)
					didx = R.drawable.building_pink_0;
				else{
					switch (scenario){
						case Values.SCENARIO_HELL:
							didx = R.drawable.building_pink_1_hell;
							break;
						case Values.SCENARIO_HEAVEN:
							didx = R.drawable.building_pink_1_heaven;
							break;
					}
				}
				break;
			case Values.BUILDING_YELLOW_1:
				if (box[_box].getAge() < Values.AGE_1)
					didx = R.drawable.building_yellow_0;
				else{
					switch (scenario){
						case Values.SCENARIO_HELL:
							didx = R.drawable.building_yellow_1_hell;
							break;
						case Values.SCENARIO_HEAVEN:
							didx = R.drawable.building_yellow_1_heaven;
							break;
					}
				}
				break;
			case Values.BUILDING_BLUE_1:
				if (box[_box].getAge() < Values.AGE_1)
					didx = R.drawable.building_blue_0;
				else{
					switch (scenario){
						case Values.SCENARIO_HELL:
							didx = R.drawable.building_blue_1_hell;
							break;
						case Values.SCENARIO_HEAVEN:
							didx = R.drawable.building_blue_1_heaven;
							break;
					}
				}
				break;
			case Values.BUILDING_RED_1:
				if (box[_box].getAge() < Values.AGE_1)
					didx = R.drawable.building_red_0;
				else{
					switch (scenario){
						case Values.SCENARIO_HELL:
							didx = R.drawable.building_red_1_hell;
							break;
						case Values.SCENARIO_HEAVEN:
							didx = R.drawable.building_red_1_heaven;
							break;
					}
				}
				break;
			case Values.BUILDING_GREEN_1:
				if (box[_box].getAge() < Values.AGE_1)
					didx = R.drawable.building_green_0;
				else{
					switch (scenario){
						case Values.SCENARIO_HELL:
							didx = R.drawable.building_green_1_hell;
							break;
						case Values.SCENARIO_HEAVEN:
							didx = R.drawable.building_green_1_heaven;
							break;
					}
				}
				break;
			case Values.BUILDING_PURPLE_1:
				if (box[_box].getAge() < Values.AGE_1)
					didx = R.drawable.building_purple_0;
				else{
					switch (scenario){
						case Values.SCENARIO_HELL:
							didx = R.drawable.building_purple_1_hell;
							break;
						case Values.SCENARIO_HEAVEN:
							didx = R.drawable.building_purple_1_heaven;
							break;
					}
				}
				break;
			//Level 2 areas
			case Values.BUILDING_MAROON_2_BAD:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_maroon_2_bad_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_maroon_2_bad_heaven;
						break;
				}
				break;
			case Values.BUILDING_MAROON_2_NEUTRAL:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_maroon_2_neutral_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_maroon_2_neutral_heaven;
						break;
				}
				break;
			case Values.BUILDING_MAROON_2_GOOD:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_maroon_2_good_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_maroon_2_good_heaven;
						break;
				}
				break;
			case Values.BUILDING_PINK_2_BAD:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_pink_2_bad_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_pink_2_bad_heaven;
						break;
				}
				break;
			case Values.BUILDING_PINK_2_NEUTRAL:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_pink_2_neutral_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_pink_2_neutral_heaven;
						break;
				}
				break;
			case Values.BUILDING_PINK_2_GOOD:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_pink_2_good_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_pink_2_good_heaven;
						break;
				}
				break;
			case Values.BUILDING_YELLOW_2_BAD:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_yellow_2_bad_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_yellow_2_bad_heaven;
						break;
				}
				break;
			case Values.BUILDING_YELLOW_2_NEUTRAL:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_yellow_2_neutral_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_yellow_2_neutral_heaven;
						break;
				}
				break;
			case Values.BUILDING_YELLOW_2_GOOD:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_yellow_2_good_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_yellow_2_good_heaven;
						break;
				}
				break;
			case Values.BUILDING_BLUE_2_BAD:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_blue_2_bad_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_blue_2_bad_heaven;
						break;
				}
				break;
			case Values.BUILDING_BLUE_2_NEUTRAL:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_blue_2_neutral_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_blue_2_neutral_heaven;
						break;
				}
				break;
			case Values.BUILDING_BLUE_2_GOOD:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_blue_2_good_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_blue_2_good_heaven;
						break;
				}
				break;
			case Values.BUILDING_RED_2_BAD:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_red_2_bad_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_red_2_bad_heaven;
						break;
				}
				break;
			case Values.BUILDING_RED_2_NEUTRAL:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_red_2_neutral_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_red_2_neutral_heaven;
						break;
				}
				break;
			case Values.BUILDING_RED_2_GOOD:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_red_2_good_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_red_2_good_heaven;
						break;
				}
				break;
			case Values.BUILDING_GREEN_2_BAD:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_green_2_bad_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_green_2_bad_heaven;
						break;
				}
				break;
			case Values.BUILDING_GREEN_2_NEUTRAL:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_green_2_neutral_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_green_2_neutral_heaven;
						break;
				}
				break;
			case Values.BUILDING_GREEN_2_GOOD:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_green_2_good_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_green_2_good_heaven;
						break;
				}
				break;
			case Values.BUILDING_PURPLE_2_BAD:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_purple_2_bad_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_purple_2_bad_heaven;
						break;
				}
				break;
			case Values.BUILDING_PURPLE_2_NEUTRAL:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_purple_2_neutral_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_purple_2_neutral_heaven;
						break;
				}
				break;
			case Values.BUILDING_PURPLE_2_GOOD:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_purple_2_good_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_purple_2_good_heaven;
						break;
				}
				break;
				//Level 3 areas
				case Values.BUILDING_MAROON_3_BAD:
					switch (scenario){
						case Values.SCENARIO_HELL:
							didx = R.drawable.building_maroon_3_bad_hell;
							break;
						case Values.SCENARIO_HEAVEN:
							didx = R.drawable.building_maroon_3_bad_heaven;
							break;
					}
					break;
				case Values.BUILDING_MAROON_3_NEUTRAL:
					switch (scenario){
						case Values.SCENARIO_HELL:
							didx = R.drawable.building_maroon_3_neutral_hell;
							break;
						case Values.SCENARIO_HEAVEN:
							didx = R.drawable.building_maroon_3_neutral_heaven;
							break;
					}
					break;
				case Values.BUILDING_MAROON_3_GOOD:
					switch (scenario){
						case Values.SCENARIO_HELL:
							didx = R.drawable.building_maroon_3_good_hell;
							break;
						case Values.SCENARIO_HEAVEN:
							didx = R.drawable.building_maroon_3_good_heaven;
							break;
					}
					break;
				case Values.BUILDING_PINK_3_BAD:
					switch (scenario){
						case Values.SCENARIO_HELL:
							didx = R.drawable.building_pink_3_bad_hell;
							break;
						case Values.SCENARIO_HEAVEN:
							didx = R.drawable.building_pink_3_bad_heaven;
							break;
					}
					break;
				case Values.BUILDING_PINK_3_NEUTRAL:
					switch (scenario){
						case Values.SCENARIO_HELL:
							didx = R.drawable.building_pink_3_neutral_hell;
							break;
						case Values.SCENARIO_HEAVEN:
							didx = R.drawable.building_pink_3_neutral_heaven;
							break;
					}
					break;
				case Values.BUILDING_PINK_3_GOOD:
					switch (scenario){
						case Values.SCENARIO_HELL:
							didx = R.drawable.building_pink_3_good_hell;
							break;
						case Values.SCENARIO_HEAVEN:
							didx = R.drawable.building_pink_3_good_heaven;
							break;
					}
					break;
				case Values.BUILDING_YELLOW_3_BAD:
					switch (scenario){
						case Values.SCENARIO_HELL:
							didx = R.drawable.building_yellow_3_bad_hell;
							break;
						case Values.SCENARIO_HEAVEN:
							didx = R.drawable.building_yellow_3_bad_heaven;
							break;
					}
					break;
				case Values.BUILDING_YELLOW_3_NEUTRAL:
					switch (scenario){
						case Values.SCENARIO_HELL:
							didx = R.drawable.building_yellow_3_neutral_hell;
							break;
						case Values.SCENARIO_HEAVEN:
							didx = R.drawable.building_yellow_3_neutral_heaven;
							break;
					}
					break;
				case Values.BUILDING_YELLOW_3_GOOD:
					switch (scenario){
						case Values.SCENARIO_HELL:
							didx = R.drawable.building_yellow_3_good_hell;
							break;
						case Values.SCENARIO_HEAVEN:
							didx = R.drawable.building_yellow_3_good_heaven;
							break;
					}
					break;
				case Values.BUILDING_BLUE_3_BAD:
					switch (scenario){
						case Values.SCENARIO_HELL:
							didx = R.drawable.building_blue_3_bad_hell;
							break;
						case Values.SCENARIO_HEAVEN:
							didx = R.drawable.building_blue_3_bad_heaven;
							break;
					}
					break;
				case Values.BUILDING_BLUE_3_NEUTRAL:
					switch (scenario){
						case Values.SCENARIO_HELL:
							didx = R.drawable.building_blue_3_neutral_hell;
							break;
						case Values.SCENARIO_HEAVEN:
							didx = R.drawable.building_blue_3_neutral_heaven;
							break;
					}
					break;
				case Values.BUILDING_BLUE_3_GOOD:
					switch (scenario){
						case Values.SCENARIO_HELL:
							didx = R.drawable.building_blue_3_good_hell;
							break;
						case Values.SCENARIO_HEAVEN:
							didx = R.drawable.building_blue_3_good_heaven;
							break;
					}
					break;
				case Values.BUILDING_RED_3_BAD:
					switch (scenario){
						case Values.SCENARIO_HELL:
							didx = R.drawable.building_red_3_bad_hell;
							break;
						case Values.SCENARIO_HEAVEN:
							didx = R.drawable.building_red_3_bad_heaven;
							break;
					}
					break;
				case Values.BUILDING_RED_3_NEUTRAL:
					switch (scenario){
						case Values.SCENARIO_HELL:
							didx = R.drawable.building_red_3_neutral_hell;
							break;
						case Values.SCENARIO_HEAVEN:
							didx = R.drawable.building_red_3_neutral_heaven;
							break;
					}
					break;
				case Values.BUILDING_RED_3_GOOD:
					switch (scenario){
						case Values.SCENARIO_HELL:
							didx = R.drawable.building_red_3_good_hell;
							break;
						case Values.SCENARIO_HEAVEN:
							didx = R.drawable.building_red_3_good_heaven;
							break;
					}
					break;
				case Values.BUILDING_GREEN_3_BAD:
					switch (scenario){
						case Values.SCENARIO_HELL:
							didx = R.drawable.building_green_3_bad_hell;
							break;
						case Values.SCENARIO_HEAVEN:
							didx = R.drawable.building_green_3_bad_heaven;
							break;
					}
					break;
				case Values.BUILDING_GREEN_3_NEUTRAL:
					switch (scenario){
						case Values.SCENARIO_HELL:
							didx = R.drawable.building_green_3_neutral_hell;
							break;
						case Values.SCENARIO_HEAVEN:
							didx = R.drawable.building_green_3_neutral_heaven;
							break;
					}
					break;
				case Values.BUILDING_GREEN_3_GOOD:
					switch (scenario){
						case Values.SCENARIO_HELL:
							didx = R.drawable.building_green_3_good_hell;
							break;
						case Values.SCENARIO_HEAVEN:
							didx = R.drawable.building_green_3_good_heaven;
							break;
					}
					break;
				case Values.BUILDING_PURPLE_3_BAD:
					switch (scenario){
						case Values.SCENARIO_HELL:
							didx = R.drawable.building_purple_3_bad_hell;
							break;
						case Values.SCENARIO_HEAVEN:
							didx = R.drawable.building_purple_3_bad_heaven;
							break;
					}
					break;
				case Values.BUILDING_PURPLE_3_NEUTRAL:
					switch (scenario){
						case Values.SCENARIO_HELL:
							didx = R.drawable.building_purple_3_neutral_hell;
							break;
						case Values.SCENARIO_HEAVEN:
							didx = R.drawable.building_purple_3_neutral_heaven;
							break;
					}
					break;
				case Values.BUILDING_PURPLE_3_GOOD:
					switch (scenario){
						case Values.SCENARIO_HELL:
							didx = R.drawable.building_purple_3_good_hell;
							break;
						case Values.SCENARIO_HEAVEN:
							didx = R.drawable.building_purple_3_good_heaven;
							break;
					}
					break;
			//Level 1 buildings. Must switch Hell/Heaven
			case Values.BUILDING_DOOR_1:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_door_1_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_door_1_heaven;
						break;
				}
				break;
			case Values.BUILDING_ACADEMY_1:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_academy_1_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_academy_1_heaven;
						break;
				}
				break;
			case Values.BUILDING_STATION_1:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_station_1_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_station_1_heaven;
						break;
				}
				break;
			case Values.BUILDING_TOPIA_1:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_topia_1_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_topia_1_heaven;
						break;
				}
				break;
			case Values.BUILDING_SIPHON_1:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_siphon_1_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_siphon_1_heaven;
						break;
				}
				break;
			case Values.BUILDING_SPECIAL_1:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_special_1_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_special_1_heaven;
						break;
				}
				break;
			//Level 2 buildings. Must switch Hell/Heaven
			case Values.BUILDING_DOOR_2:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_door_2_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_door_2_heaven;
						break;
				}
				break;
			case Values.BUILDING_ACADEMY_2:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_academy_2_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_academy_2_heaven;
						break;
				}
				break;
			case Values.BUILDING_STATION_2:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_station_2_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_station_2_heaven;
						break;
				}
				break;
			case Values.BUILDING_TOPIA_2:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_topia_2_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_topia_2_heaven;
						break;
				}
				break;
			case Values.BUILDING_SIPHON_2:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_siphon_2_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_siphon_2_heaven;
						break;
				}
				break;
			case Values.BUILDING_SPECIAL_2:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_special_2_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_special_2_heaven;
						break;
				}
				break;
			//Level 2 buildings. Must switch Hell/Heaven
			case Values.BUILDING_DOOR_3:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_door_3_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_door_3_heaven;
						break;
				}
				break;
			case Values.BUILDING_ACADEMY_3:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_academy_3_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_academy_3_heaven;
						break;
				}
				break;
			case Values.BUILDING_STATION_3:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_station_3_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_station_3_heaven;
						break;
				}
				break;
			case Values.BUILDING_TOPIA_3:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_topia_3_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_topia_3_heaven;
						break;
				}
				break;
			case Values.BUILDING_SIPHON_3:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_siphon_3_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_siphon_3_heaven;
						break;
				}
				break;
			case Values.BUILDING_SPECIAL_3:
				switch (scenario){
					case Values.SCENARIO_HELL:
						didx = R.drawable.building_special_3_hell;
						break;
					case Values.SCENARIO_HEAVEN:
						didx = R.drawable.building_special_3_heaven;
						break;
				}
				break;
			}
		//Draw the building
		draw[_box] = BitmapFactory.decodeResource(getResources(), didx);
		//Invalidate the view, so it has to be draw again
		invalidate();
	}

	public void destroy(int _box){
		box[_box].setBuilding(Values.BUILDING_NONE);
		draw[_box] = null;
		box[_box].reset();
		for (int i = 0; i < 7; i ++)
			box[_box].setHosting(i, 0);
		
		invalidate();
	}
	
	//Called every time the board is drawn
	@Override
	public void onDraw(Canvas canvas)
	{
		//Draw Background
		canvas.drawBitmap(bgImage, 0 - scrollX, 0 - scrollY, null);
		//Draw money, year, soul text
		canvas.drawText(msgYear, 10, screenHeight - 80, textYear);
		canvas.drawText(msgMoney, (int) (screenWidth * .33), screenHeight - 80, textMoney);
		canvas.drawText(msgSouls, (int) (screenWidth * .7), screenHeight - 80, textSouls);
		//Draw the buildings
		for (int i = 0; i < 144; i++){
			if (box[i].getBuilding() != Values.BUILDING_NONE)
				canvas.drawBitmap(draw[i], box[i].drawX - scrollX, box[i].drawY - scrollY, null);
		}

	}

	//Update scrolling values when moving across the screen
	public void handleScroll(float distX, float distY)
	{
		// X-Axis
		scrollX += distX;
		if (scrollX < 0)
			scrollX = 0;
		if (scrollX > bgWidth - screenWidth)
			scrollX = bgWidth - screenWidth;

	    // Y-AXIS
		scrollY += distY;
		if (scrollY < 0)
			scrollY = 0;
		if (scrollY > bgHeight - screenHeight)
			scrollY = bgHeight - screenHeight;
		invalidate();
	 }
	
	//Not used, but must me here
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,	int height) {	}

	//Not used, but must me here
	@Override
	public void surfaceCreated(SurfaceHolder holder) {	}

	//Not used, but must me here
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {	}
	
	//Calls each box to see if given coordinates are contained in it
	public int locateTouch(int x, int y){
		for (int i = 0; i < 144; i ++){
			if (box[i].coordinatesInBox(x + scrollX, y + scrollY, i) == true)
					return i;
		}
		return - 1;
	}

	//Getters and setters
	public int getScenario() {
		return scenario;
	}

	public void setScenario(byte scenario) {
		this.scenario = scenario;
	}
	
	public int[] getScreenDimensions(){
		int [] dim = new int[2];
		dim[0] = screenWidth;
		dim[1] = screenHeight;
		return dim;
	}
	
	public void clear(){
		bgImage = null;
		for (int i = 0; i < 144; i ++)
			draw[i] = null;
	}
}